package com.example.caimogu.fragment;


import android.content.Context;
import android.os.Bundle;
import android.app.Fragment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.example.caimogu.bean.Goods;
import com.example.caimogu.bean.Order;
import com.example.caimogu.businessapp.MainActivity;
import com.example.caimogu.businessapp.R;
import com.example.caimogu.fragmentAdapter.UnhandledOrderAdapter;
import com.example.caimogu.fragmentImpl.UnhandledOrderImpl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import java.util.logging.LogRecord;

/**
 * Created by Caimogu on 2016/4/20.
 */
public class UnhandledOrder extends Fragment {

    private Context context;
    private Button btn_getOrders;
    private ListView listView;
    private UnhandledOrderAdapter unhandledOrderAdapter;
    private LayoutInflater layoutInflater;
    private  List<Order> orders = null;

    public UnhandledOrder(Context context, LayoutInflater layoutInflater){
        this.context = context;
        this.layoutInflater = layoutInflater;
    }

    private List<Goods> goodsList=new ArrayList<Goods>();
//    private  void initData(){
//        //int[] ii=new int[]{R.drawable.a24,R.drawable.a9};
//        for(int i=0;i<10;i++){
//            Goods goods=new Goods();
//            goods.setName("此处无订单");
//            goods.setInformation("此处无信息");
//            //f.setSnap(ii[i]);
//            goodsList.add(goods);
//        }
//    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.unhandledorder,container,false);

        //initData();
        init(view);


        listView = (ListView)view.findViewById(R.id.lv_uo_orderlist);

        return view;
    }

    private void init(View view) {
//         unhandledOrderAdapter = new UnhandledOrderAdapter(goodsList,layoutInflater);
        //listView.setAdapter(unhandledOrderAdapter);
//        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//
//            }
//        });
        btn_getOrders = (Button)view.findViewById(R.id.btn_uo_getorders);
        btn_getOrders.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getOrdersById();
            }
        });
    }

    public void getOrdersById() {
        //Toast.makeText(context, " 收到订单 ", Toast.LENGTH_SHORT).show();
        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.i("caimogu", "getOrdersById..");
                UnhandledOrderImpl unhandledOrderImpl = new UnhandledOrderImpl();
                String bid = ((MainActivity)getActivity()).getOnlyBid();
                orders = unhandledOrderImpl.getOrdersByIdImpl(bid);
                for(Order o: orders){
                    Log.i("caimogu",o.getCid()+o.getGoodsIDs());
                }
                Message message = new Message();
                message.what = 1;
//                Bundle b = new Bundle();
//                ArrayList list = new ArrayList();
//                list.add(orders);
//                b.putParcelableArrayList("orders", list);
//                message.setData(b);
                handler.sendMessage(message);

            }
        }).start();
    }


    private final Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(msg.what == 1){
//                Bundle b = msg.getData();
//                List<Order> orders = (ArrayList)b.getSerializable("orders");
                if(orders != null) {
                    unhandledOrderAdapter = new UnhandledOrderAdapter(orders, layoutInflater,context);
                    listView.setAdapter(unhandledOrderAdapter);
                }else{
                    Toast.makeText(context, " 暂无订单 ", Toast.LENGTH_SHORT).show();
                }

            }
        }
    };


}
